package com.ray.depot.service.impl;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.ray.depot.service.MaterialPropertyService;
import com.ray.depot.service.MaterialService;
import com.ray.depot.table.entity.Material;
import com.ray.depot.table.entity.MaterialExample;
import com.ray.depot.table.entity.MaterialExample.Criteria;
import com.ray.depot.table.mapper.MaterialMapper;
import com.zjhc.flyhong.common.function.enums.DeleteEnum;
import com.zjhc.flyhong.common.function.exception.BussinessException;
import com.zjhc.flyhong.common.function.operator.LoginUser;
import com.zjhc.flyhong.common.function.operator.PageDto;
import com.zjhc.flyhong.common.util.UUIDUtil;
import com.zjhc.flyhong.common.web.ErrorMsg;
import com.zjhc.flyhong.common.web.Result;
import com.zjhc.flyhong.common.web.SuccessMsg;
import com.zjhc.flyhong.common.web.base.BaseService;
@Service
public class MaterialServiceImpl extends BaseService implements MaterialService{
	/**日志对象**/
	private static Logger logger = Logger.getLogger(MaterialServiceImpl.class);
	
	@Autowired
	private MaterialMapper materialMapper;
	@Autowired
	private MaterialPropertyService materialPropertyService;

	@Transactional
	public Result<Object> addMaterial(Material params, LoginUser user) {
		Result<Object> result=null;
		result=new Result<Object>(0,SuccessMsg.OP_SUCCESS);
		params.setTenantId(user.getTenantId());
		params.setMaterialId(UUIDUtil.getUUID());
		params.setIsDelete(DeleteEnum.USE.getValue());
		this.setUserForAdd(params, user);
		int count = materialMapper.insertSelective(params);
		if(count!=1){
			logger.error("MaterialServiceImpl=>addMaterial is error");
			throw new BussinessException(-1, ErrorMsg.INSERT_ERROR);
		}
		return result;
	}
	
	
	@Transactional(propagation=Propagation.NOT_SUPPORTED)
	public Material queryMaterial(String id, LoginUser user) {
		return materialMapper.selectByPrimaryKey(id);
	}
	
	
    @Transactional
	public Result<Object> updateMaterial(Material params, LoginUser user) {
		Result<Object> result=null;
		result=new Result<Object>(0,SuccessMsg.OP_SUCCESS);
		this.setUserForUpdate(params, user);
		int count=materialMapper.updateByPrimaryKeySelective(params);
		if(count!=1){
			logger.error("MaterialServiceImpl=>editMaterial is error");
			throw new BussinessException(-1, ErrorMsg.INSERT_ERROR);
		}
		return result;
	}
    
    @Transactional
	public Result<Object> deleteMaterial(String id, LoginUser user) {
		Result<Object> result=null;
		result=new Result<Object>(0,SuccessMsg.OP_SUCCESS);
		Material params=new Material();
		params.setMaterialId(id);
		params.setIsDelete(DeleteEnum.DELETE.getValue());
		this.setUserForUpdate(params, user);
		int count=materialMapper.updateByPrimaryKeySelective(params);
		if(count!=1){
			logger.error("MaterialServiceImpl=>deleteMaterial is error");
			throw new BussinessException(-1, ErrorMsg.INSERT_ERROR);
		}
		//删除物料后 一起删除物料属性表
		materialPropertyService.deleteMaterialProprety(id, user);
		return result;
	}
    
    @Transactional(propagation=Propagation.NOT_SUPPORTED)
	public PageDto<Material> queryMaterial(Material params, PageDto<Material> page, LoginUser user) {
		MaterialExample example=new MaterialExample();
		Criteria criteria = example.createCriteria();
		//超级管理员能查看全部
		if(!user.isSuperManager()){
			criteria.andTenantIdEqualTo(user.getTenantId());
		}
		criteria.andIsDeleteEqualTo(DeleteEnum.USE.getValue());
		this.setExamplePage(example, page);
		page.setTotal(materialMapper.countByExample(example));
		page.setRecords(materialMapper.selectByExample(example));
		return page;
	}

}
